extends Area2D

@onready var timer = $Timer
@onready var bullet = load("res://Bullet/tscn/Bullet.tscn")
@onready var util = Util.new()
@onready var marksman = $".."

var body = null
var vector2 = Vector2.ZERO

func _ready():
	connect("area_entered", areaEntered)
	connect("area_exited", areaExited)
	timer.connect("timeout", timeout)
	timer.start()
	
func areaEntered(area):
	self.body = area.get_parent()

func areaExited(area):
	self.body = null

func timeout():
	# 对玩家开火
	if body != null:
		var bullet = bullet.instantiate()
		# 为子弹复制属性
		bullet.speed = 1
		bullet.power = 1
		# 修改子弹的初始位置
		bullet.position = marksman.global_position
		bullet.direction = body.global_position - global_position
		var bulletHitBox = util.findOne("$[BulletHitBox]", bullet)
		bulletHitBox.collision_layer = 12
		bulletHitBox.collision_mask = 1
		get_tree().root.add_child(bullet)
		
	
